import Vue from 'vue'
import VueRouter from 'vue-router'
import Home from '../views/Home.vue'
import Login from '@/views/Login'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: 'Home',
    component: Home,
    redirect:'/about',
    children: [
      {
        path: "about",
        name: "About",
        component: () => import('../views/About'),
        meta:{
          title:"首页"
        }
      },
      {
        path: "user",

        name: "User",
        component: () => import('../views/user/User'),
        meta:{
          title:'用户管理',
          parentTitle:'系统管理'
        }
      },
      {
        path: "role",
        name: "Role",
        component: () => import('../views/role/Role'),
        meta:{
          title:'角色管理',
          parentTitle:'系统管理'
        }
      },
      {
        path: "dept",
        name: "Dept",
        component: () => import('../views/dept/Dept'),
        meta:{
          title:'部门管理',
          parentTitle:'系统管理'
        }
      },
      {
        path: "shop",
        name:"Shop",
        component: () => import('../views/shop/Shop'),
        meta:{
          title:'商户管理',
          parentTitle:'商家管理'
        }

      },
      {
        path:'shopEdit',
        name:"ShopEdit",
        component: () => import('../views/shop/ShopEdit'),
        meta:{
          title:'商户编辑',
          parentTitle:'商家管理'
        }
      },
      {
        path: 'food',
        name:"foodEdit",
        component: () => import('../views/shop/food/FoodEdit'),
        meta:{
          title:'食物编辑',
          parentTitle:'商家管理'
        }
      },
      {
        path: 'foodList',
        name:"Food",
        component: () => import('../views/shop/food/Food'),
        meta:{
          title:'食物列表',
          parentTitle:'商家管理'
        }
      },

      {
        path: "listShop",
        name:"ListShop",
        component: () => import('../views/shop/listShop')
      },

      {
        path: "addShop",
        name:"AddShop",
        component: () => import('../views/shop/addShop')
      },

    ]
  }, {
    name: 'Login',
    path: '/login',
    component: Login
  }
]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  // console.log(to.path.substr(1));
  router.app.$options.store.state.defaultActive=to.path.substr(1)


  if (to.path === '/login') {
    next()
  } else {
    if (Vue.$cookies.isKey('token')) {
      next()
    } else {
      next({path: '/login'})
    }
  }
})


const VueRouterPush = VueRouter.prototype.push
VueRouter.prototype.push = function push (to) {
  return VueRouterPush.call(this, to).catch(err => err)
}


export default router



